import os
import glob

def compute_result(n, m, k, a_list):
    total = sum(a_list)
    t = total // k
    # 如果 t < m+1 输出 t，否则输出 m+1
    if t < m + 1:
        return t
    else:
        return m + 1


def main():
    # 输入和输出目录
    input_dir = r"C:\Users\ASUS\Desktop\2025-BBUCPC\2025校赛出题\A题\A_inputs"
    output_dir = r"C:\Users\ASUS\Desktop\2025-BBUCPC\2025校赛出题\A题\A_outputs"
    os.makedirs(output_dir, exist_ok=True)

    # 查找所有 .in 文件，并按数字顺序处理
    pattern = os.path.join(input_dir, "*.in")
    files = sorted(glob.glob(pattern), key=lambda x: int(os.path.splitext(os.path.basename(x))[0]))

    for infile in files:
        # 读取输入
        with open(infile, 'r', encoding='utf-8') as f:
            parts = f.read().strip().split()
        n = int(parts[0])
        m = int(parts[1])
        k = int(parts[2])
        a = list(map(int, parts[3:3 + n]))

        # 计算结果
        res = compute_result(n, m, k, a)

        # 写入对应输出文件
        basename = os.path.splitext(os.path.basename(infile))[0]
        outfile = os.path.join(output_dir, f"{basename}.out")
        with open(outfile, 'w', encoding='utf-8') as f:
            f.write(str(res))

        print(f"Generated {outfile}")

if __name__ == '__main__':
    main()
